﻿using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Caching.StackExchangeRedis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BasicModuleCache;
using Microsoft.Extensions.DependencyInjection;
using BasicModuleCache.Service;

namespace BasicModuleCache.Model
{
    public class CacheRun
    {
        public void Run()
        {
            #region  MemoryCache 缓存组件
            {
                //string data = "数据";
                //string Data_Key = "Date_Key";
                //MemoryCacheOptions memoryCacheOptions = new MemoryCacheOptions();
                //IMemoryCache memoryCache = new MemoryCache(memoryCacheOptions);
                //memoryCache.Set<string>(Data_Key, data);
                //string dataCache = memoryCache.Get<string>(Data_Key);
                //Console.WriteLine(dataCache);
            }
            #endregion

            #region  Redis 缓存组件
            {
                //string data = "数据";
                //string Data_Key = "Date_Key";
                //RedisCacheOptions redisCacheOptions = new RedisCacheOptions();
                //redisCacheOptions.Configuration = "localhost:6379";
                //IDistributedCache distributedCache = new RedisCache(redisCacheOptions);
                //distributedCache.SetString(Data_Key, data);
                //string dataCache = distributedCache.GetString(Data_Key);
                //Console.WriteLine(dataCache);
            }
            #endregion

            #region  缓存自定义-字典存储
            {
                //// 1、创建数据
                //string Data = "数据";
                //string Date_Kay = "Date_Kay";

                //// 2、创建自定义缓存CustomCache
                //IDistributedCache distributedCache = new CustomCache();

                //// 3、存储数据
                //byte[] bytes = Encoding.UTF8.GetBytes(Data);
                //distributedCache.Set(Date_Kay, bytes);

                //// 4、取数据
                //byte[] bytes1 = distributedCache.Get(Date_Kay);
                //var bytes2 = distributedCache.GetAsync(Date_Kay).Result;
                //string DataString = Encoding.UTF8.GetString(bytes2);

                //Console.WriteLine($"DataString:{DataString}");
            }
            #endregion

            #region  缓存自定义-MySQL存储
            {
                //// 1、创建数据
                //string Data = "数据";
                //string Date_Kay = "Date_Kay";

                //// 2、创建自定义缓存MySQLCustomCache
                //IDistributedCache distributedCache = new MysqlCache();

                //// 3、存储数据
                //byte[] bytes = Encoding.UTF8.GetBytes(Data);
                //distributedCache.Set(Date_Kay, bytes);

                //// 4、取数据
                //byte[] bytes1 = distributedCache.Get(Date_Kay);
                //string DataString = Encoding.UTF8.GetString(bytes1);

                //Console.WriteLine($"DataString:{DataString}");
            }
            #endregion

            #region  MemoryCache 缓存大小
            {
                // 1、创建数据
                string Data = "数据";
                string Date_Kay = "Date_Kay";

                // 2、创建缓存接口
                MemoryCacheOptions memoryCacheOptions = new MemoryCacheOptions();
                //memoryCacheOptions.SizeLimit = 5; // 5个缓存单位 单位跟字节大小没有直接关系 是动态的
                IMemoryCache memoryCache = new MemoryCache(memoryCacheOptions);

                // 2.1、缓存项
                MemoryCacheEntryOptions memoryCacheEntryOptions = new MemoryCacheEntryOptions();
                memoryCacheEntryOptions.SetSize(2); // 缓存数据占用的单位

                MemoryCacheEntryOptions memoryCacheEntryOptions2 = new MemoryCacheEntryOptions();
                memoryCacheEntryOptions2.SetSize(1); // 缓存数据占用的单位
                // 3、存储数据
                memoryCache.Set<string>(Date_Kay, Data, memoryCacheEntryOptions);
                memoryCache.Set<string>("Date_Kay1", "数据1", memoryCacheEntryOptions);
                memoryCache.Set<string>("Date_Kay2", "数据2", memoryCacheEntryOptions2);
                memoryCache.Set<string>("Date_Kay3", "数据3", memoryCacheEntryOptions2);

                // 4、取数据
                string DataCache = memoryCache.Get<string>(Date_Kay);
                string DataCache1 = memoryCache.Get<string>("Date_Kay1");
                string DataCache2 = memoryCache.Get<string>("Date_Kay2");
                string DataCache3 = memoryCache.Get<string>("Date_Kay3");

                Console.WriteLine($"DataCache:{DataCache}");
                Console.WriteLine($"DataCache1:{DataCache1}");
                Console.WriteLine($"DataCache2:{DataCache2}");
                Console.WriteLine($"DataCache3:{DataCache3}");
            }
            #endregion

            #region  MemoryCache 缓存时间
            {
                //// 1、创建数据
                //string Data = "数据";
                //string Date_Kay = "Date_Kay";

                //// 2、创建缓存接口
                //MemoryCacheOptions memoryCacheOptions = new MemoryCacheOptions();
                //IMemoryCache memoryCache = new MemoryCache(memoryCacheOptions);

                //// 2.1、缓存项
                //MemoryCacheEntryOptions memoryCacheEntryOptions = new MemoryCacheEntryOptions();
                //memoryCacheEntryOptions.SetAbsoluteExpiration(TimeSpan.FromSeconds(5));  //固定过期时间
                ////memoryCacheEntryOptions.SetSlidingExpiration(TimeSpan.FromSeconds(5));  //滑动过期时间

                //// 3、存储数据
                //memoryCache.Set<string>(Date_Kay, Data, memoryCacheEntryOptions);

                //// 4、取数据
                //string DataCache = memoryCache.Get<string>(Date_Kay);

                //Thread.Sleep(4000);
                //// 5、第二次取
                //DataCache = memoryCache.Get<string>(Date_Kay);

                //Thread.Sleep(4000);
                //// 6、第三次取
                //DataCache = memoryCache.Get<string>(Date_Kay);

                //// 6、第四次取
                //DataCache = memoryCache.Get<string>(Date_Kay);
                //Console.WriteLine($"DataCache:{DataCache}");
            }
            #endregion

            #region  MemoryCache 缓存并发
            {
                //// 1、创建数据
                //string Data = "数据";
                //string Date_Kay = "Date_Kay";
                //string valueData = "";

                //// 2、创建缓存接口
                //MemoryCacheOptions memoryCacheOptions = new MemoryCacheOptions();
                //IMemoryCache memoryCache = new MemoryCache(memoryCacheOptions);

                //// 2.1、缓存项
                //MemoryCacheEntryOptions memoryCacheEntryOptions = new MemoryCacheEntryOptions();
                //memoryCacheEntryOptions.SetAbsoluteExpiration(TimeSpan.FromSeconds(5));

                //// 3、存储数据
                //memoryCache.Set<string>(Date_Kay, Data, memoryCacheEntryOptions);

                //// 4、并发取数据
                //Parallel.For(1, 5, (Options) =>
                //{
                //    Thread.Sleep(4999);
                //    //多线程并发取数据用TryGetValue
                //    memoryCache.TryGetValue<string>(Date_Kay, out valueData);
                //    //string valueData = memoryCache.Get<string>(Date_Kay);
                //    Console.WriteLine($"DataCache:{valueData}");
                //});
                ////Console.ReadLine();
                //Console.WriteLine("wwww");
                //Console.WriteLine("xxxx");
            }
            #endregion

            #region  MemoryCache 缓存应用
            {
                // 1、创建IOC容器
                IServiceCollection services = new ServiceCollection();
                // 2、添加缓存组件
                services.AddMemoryCache(options => {
                    options.SizeLimit = 4; // 设置存储单位
                });

                // 3、添加ProductService
                services.AddSingleton<ProductService>();

                // 6、获取ProductService
                ProductService productService = services.BuildServiceProvider().GetRequiredService<ProductService>();

                var Products = productService.GetProduct();

                Products = productService.GetProduct();

                Products = productService.GetProduct();
                Console.ReadLine();
            }
            #endregion
        }
    }
}
